using UnityEngine;
using System.Collections;

public class ActionAnimation : ActionInvoker
{

	public Animation actionAnimation;
	public float animationSpeed;
	
	override public void run (float duration)
	{
		if (actionAnimation == null) {
			return;
		}
		
		if (duration == 0) {
			animationSpeed = 0;
			
		} else {
			animationSpeed = 1.0f / duration;
		}
		
		foreach (AnimationState animationState in actionAnimation) {
			animationState.enabled = true;
			animationState.speed = animationSpeed;
		}
	}
	
	override protected void Update ()
	{
		if (actionAnimation == null || animationSpeed == 0) {
			return;
		}
		
		bool animationEnd = true;
		foreach (AnimationState animationState in actionAnimation) {
			//animationState.time += animationState.length * Time.deltaTime * animationSpeed;
			if (animationSpeed > 0) {
				if (animationState.time > animationState.length) {
					animationState.time = animationState.length;
					
				} else {
					animationEnd = false;
				}
			} else {
				if (animationState.time < 0) {
					animationState.time = 0;
					
				} else {
					animationEnd = false;
				}
			}
		}
		
		//actionAnimation.Sample();
		if (animationEnd) {
			animationSpeed = 0;
		}
	}
	
}
